package com.prs.crm.action.crm;

import java.io.File;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.crm.Rival;
import com.prs.crm.domain.crm.RivalTrends;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.crm.RivalService;
import com.prs.crm.service.crm.RivalTrendsService;

@Entity
@Table(name = "crm_rival_trends")
public class RivalTrendsAction extends BaseAction {
	private RivalTrends rivalTrends;
	private RivalTrendsService rivalTrendsService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private Rival rival;
	private RivalService rivalService;
	private String levelIds;

	private File upload;
	private String uploadFileName;

	@Action("listRivalTrends")
	public String listRivalTrends() {
		return SUCCESS;
	}

	@Action("listRivalTrendsByLevel")
	public String listRivalTrendsByLevel() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				RivalTrends.class);
		criteria.addOrder(Order.desc("recordTime"));
		if (levelIds != null) {
			if (!"all".endsWith(levelIds)) {
				levelIds = ReportUtils.URLDecode(levelIds);
				levelIds = levelIds.split(",")[0];
				if (ToolKits.isInteger(levelIds)) {
					criteria.createAlias("rival", "ri");
					criteria.createAlias("ri.rivalLevel", "le");
					criteria.add(Restrictions.eq("le.id", Integer
							.parseInt(levelIds)));
				}
			}
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRivalTrendsService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editRivalTrends")
	public String editRivalTrends() {
		return SUCCESS;
	}

	@Action("editRivalTrendsByLevel")
	public String editRivalTrendsByLevel() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rival.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);

		if (levelIds != null) {
			if (!"all".endsWith(levelIds)) {
				levelIds = ReportUtils.URLDecode(levelIds);
				levelIds = levelIds.split(",")[0];
				if (ToolKits.isInteger(levelIds)) {
					criteria.createAlias("rivalLevel", "le");
					criteria.add(Restrictions.eq("le.id", Integer
							.parseInt(levelIds)));
				}
			}
		}

		this.setPagination(this.getRivalService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("registerRivalTrends")
	public String registerRivalTrends() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitRivalTrends", results = {
			@Result(name = "success", type = "redirect", location = "editRivalTrends"),
			@Result(name = "chain", type = "chain", location = "registerRivalTrends") })
	public String submitRivalTrends() {
		if (rivalTrends.getTitle() == null
				|| rivalTrends.getTitle().trim().equals("")) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (rivalTrends.getTrendsTime() == null) {
			addActionError("动态时间不能为空");
			return ERROR;
		}

		rivalTrends.setRival(rival);
		this.getRivalTrendsService().saveRivalTrends(rivalTrends, upload,
				uploadFileName);
		this.setPrompt("提交竞争对手动态成功");
		return SUCCESS;
	}

	@Action(value = "removeRivalTrends", results = {
			@Result(name = "success", type = "redirect", location = "alterRivalTrends"),
			@Result(name = "error", type = "chain", location = "alterRivalTrends") })
	public String removeRivalTrends() {
		if (ids == null) {
			this.addActionError("请选择要删除的对手动态");
			return ERROR;
		}
		this.getRivalTrendsService().batchRemove(ids);
		return SUCCESS;
	}

	@Action("alterRivalTrends")
	public String alterRivalTrends() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				RivalTrends.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRivalTrendsService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewRivalTrends")
	public String viewRivalTrends() {
		return SUCCESS;
	}

	public RivalTrends getRivalTrends() {
		return rivalTrends;
	}

	public void setRivalTrends(RivalTrends rivalTrends) {
		this.rivalTrends = rivalTrends;
	}

	public RivalTrendsService getRivalTrendsService() {
		return rivalTrendsService;
	}

	public void setRivalTrendsService(RivalTrendsService rivalTrendsService) {
		this.rivalTrendsService = rivalTrendsService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setRival(Rival rival) {
		this.rival = rival;
	}

	public Rival getRival() {
		return rival;
	}

	public void setRivalService(RivalService rivalService) {
		this.rivalService = rivalService;
	}

	public RivalService getRivalService() {
		return rivalService;
	}

	public String getLevelIds() {
		return levelIds;
	}

	public void setLevelIds(String levelIds) {
		this.levelIds = levelIds;
	}

}
